package system

import (
  "livefind/common/request"
  "livefind/common/response"
  "livefind/global"
  "livefind/model/system"
  systemReq "livefind/model/system/request"
  systemRes "livefind/model/system/response"
  "livefind/utils"

  "github.com/gin-gonic/gin"
  "go.uber.org/zap"
)

type SystemApiApi struct {
}

// @Tags SysApi
// @Summary 创建基础api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system.SysApi true "api路径, api中文描述, api组, 方法"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /api/createApi [post]
func (s *SystemApiApi) CreateApi(c *gin.Context) {
  var api system.SysApi
  _ = c.ShouldBindJSON(&api)
  if err := utils.Verify(api, utils.ApiVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if err := apiService.CreateApi(api); err != nil {
    global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
    response.FailWithMessage("创建失败", c)
  } else {
    response.OkWithMessage("创建成功", c)
  }
}

// @Tags SysApi
// @Summary 删除api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system.SysApi true "ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /api/deleteApi [post]
func (s *SystemApiApi) DeleteApi(c *gin.Context) {
  var api system.SysApi
  _ = c.ShouldBindJSON(&api)
  if err := utils.Verify(api.GVA_MODEL, utils.IdVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if err := apiService.DeleteApi(api); err != nil {
    global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
    response.FailWithMessage("删除失败", c)
  } else {
    response.OkWithMessage("删除成功", c)
  }
}

// @Tags SysApi
// @Summary 分页获取API列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body systemReq.SearchApiParams true "分页获取API列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiList [post]
func (s *SystemApiApi) GetApiList(c *gin.Context) {
  var pageInfo systemReq.SearchApiParams
  _ = c.ShouldBindJSON(&pageInfo)
  if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if err, list, total := apiService.GetAPIInfoList(pageInfo.SysApi, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); err != nil {
    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
    response.FailWithMessage("获取失败", c)
  } else {
    response.OkWithDetailed(response.PageResult{
      List:     list,
      Total:    total,
      Page:     pageInfo.Page,
      PageSize: pageInfo.PageSize,
    }, "获取成功", c)
  }
}

// @Tags SysApi
// @Summary 根据id获取api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取api"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiById [post]
func (s *SystemApiApi) GetApiById(c *gin.Context) {
  var idInfo request.GetById
  _ = c.ShouldBindJSON(&idInfo)
  if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  err, api := apiService.GetApiById(idInfo.ID)
  if err != nil {
    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
    response.FailWithMessage("获取失败", c)
  } else {
    response.OkWithData(systemRes.SysAPIResponse{Api: api}, c)
  }
}

// @Tags SysApi
// @Summary 创建基础api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system.SysApi true "api路径, api中文描述, api组, 方法"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /api/updateApi [post]
func (s *SystemApiApi) UpdateApi(c *gin.Context) {
  var api system.SysApi
  _ = c.ShouldBindJSON(&api)
  if err := utils.Verify(api, utils.ApiVerify); err != nil {
    response.FailWithMessage(err.Error(), c)
    return
  }
  if err := apiService.UpdateApi(api); err != nil {
    global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
    response.FailWithMessage("修改失败", c)
  } else {
    response.OkWithMessage("修改成功", c)
  }
}

// @Tags SysApi
// @Summary 获取所有的Api 不分页
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getAllApis [post]
func (s *SystemApiApi) GetAllApis(c *gin.Context) {
  if err, apis := apiService.GetAllApis(); err != nil {
    global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
    response.FailWithMessage("获取失败", c)
  } else {
    response.OkWithDetailed(systemRes.SysAPIListResponse{Apis: apis}, "获取成功", c)
  }
}

// @Tags SysApi
// @Summary 删除选中Api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /api/deleteApisByIds [delete]
func (s *SystemApiApi) DeleteApisByIds(c *gin.Context) {
  var ids request.IdsReq
  _ = c.ShouldBindJSON(&ids)
  if err := apiService.DeleteApisByIds(ids); err != nil {
    global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
    response.FailWithMessage("删除失败", c)
  } else {
    response.OkWithMessage("删除成功", c)
  }
}
